package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.credit.*;
import com.miplus.ccrm.core.mapper.AiListMapper;
import com.miplus.ccrm.core.pojo.CreditAccount;
import com.miplus.ccrm.core.mapper.CreditAccountMapper;
import com.miplus.ccrm.core.pojo.CreditAccountBalance;
import com.miplus.ccrm.core.pojo.CreditCard;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author pc
* @description 针对表【credit_account(信用卡账户表)】的数据库操作Service实现
* @createDate 2024-03-25 14:19:34
*/
@Service
public class CreditAccountServiceImpl extends MPJBaseServiceImpl<CreditAccountMapper, CreditAccount>
    implements CreditAccountService{

    @Resource
    private AiListMapper aiListMapper;

    @Override
    public IPage<CrdAccResVo> crdAccountList(JSysUser user, CrdAccReqVo paras)
    {
        String cardstar= JUsrPerm.getPermission(user,JUsrPerm.DISSTAR_CARD);
        String $usrid=user.getUsrid();

        //总欠款本金=已出账单本金+未出账单本金+未摊销本金（settled_bill_prin+unsettled_bill_prin+unamort_prin）
        //总欠款利息=已出账单利息+未出账单利息（settled_bill_int+unsettled_bill_int）
        //总欠款费用=已出账单费用+未出账单费用+未摊销费用（settled_bill_fee+unsettled_bill_fee+unamort_expense）
        //富邦的还款日是在账单日上固定+20天，2024-08-27
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CrdAccResVo.class,
                new MPJLambdaWrapper<CreditAccount>()
                        .selectAs("fun_getnames('BUSINESS.TYPE',t.acct_big_type)", CrdAccResVo::getProdtype)
                        .selectAs("fun_getnames('CREDIT.ACCT.TYPE',t.acct_type)", CrdAccResVo::getAcctTypedsc)
                        .selectAs("fun_getnames('CREDIT.ACCT.STATUS',t.acct_status)", CrdAccResVo::getAcctStatusdsc)
                        .selectAs("(case when t.repay_date is null then date_add(STR_TO_DATE(concat(DATE_FORMAT(CURDATE(),'%Y-%m-'),t.bill_date),'%Y-%m-%d'),INTERVAL 20 DAY) end)",CrdAccResVo::getRepayDate)
                        .selectAs("(case when cab.settled_bill_prin is null then 0 else cab.settled_bill_prin end+case when cab.unsettled_bill_prin is null then 0 else cab.unsettled_bill_prin end+case when cab.unamort_prin is null then 0 else cab.unamort_prin end)", CrdAccResVo::getDebtAllPrin)
                        .selectAs("(case when cab.settled_bill_int is null then 0 else cab.settled_bill_int end+case when cab.unsettled_bill_int is null then 0 else cab.unsettled_bill_int end)", CrdAccResVo::getDebtAllInt)
                        .selectAs("(case when cab.settled_bill_fee is null then 0 else cab.settled_bill_fee end+case when cab.unsettled_bill_fee is null then 0 else cab.unsettled_bill_fee end+case when cab.unamort_expense is null then 0 else cab.unamort_expense end)", CrdAccResVo::getDebtAllFee)
                        .selectAs(String.format("fi_starSecu('%s','%s',t.crdt_card_repe_no,'%s')",$usrid,JUsrPerm.DISSTAR_CARD,cardstar), CrdAccResVo::getCrdtCardRepeNo)
                        .selectAs(CreditAccountBalance::getAge1Age6, CrdAccResVo::getOdudPrin)
                        .leftJoin(CreditAccountBalance.class,"cab",CreditAccountBalance::getAcct,CreditAccount::getAcct)
                        .select(CreditAccount::getAcct,CreditAccount::getAcctType,CreditAccount::getTempLimit,CreditAccount::getCrdtLimit,CreditAccount::getIsAmortCust,
                                CreditAccount::getAcctBigType,CreditAccount::getAcctStatus,CreditAccount::getSecondCurrency,CreditAccount::getCurrency,
                                CreditAccount::getBillDate, CreditAccount::getMddt)
                        .selectAll(CreditAccountBalance.class)
                        .eq(CreditCard::getAcct,paras.getAcctno())
        );
    }

    @Override
    public String getFirstCardNo(String acctno)
    {
        CreditAccount acc=baseMapper.selectOne(new QueryWrapper<CreditAccount>()
                .eq("acct",acctno));
        if(acc==null) return "";
        return acc.getCrdtCardRepeNo();
    }

    @Override
    public IPage<AiListResVo> queryAiList(AiListReqVo paras)
    {
        String tempid=paras.getTempid();
        if(JPubUtils.IsEqual(tempid,"ailist-ys")){
            return aiListMapper.quyAiListYs(new Page<>(paras.getPageNum(), paras.getPageSize()));
        }
        else if(JPubUtils.IsEqual(tempid,"ailist-mt")) {
            return aiListMapper.quyAiListMt(new Page<>(paras.getPageNum(), paras.getPageSize()));
        }
        else{
            return aiListMapper.quyAiListCar(new Page<>(paras.getPageNum(), paras.getPageSize()));
        }
    }
}




